﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace DataFileApplication_WPF.Commands
{
    public class DelegateCommand : ICommand
    {
        //private readonly Action<object> _execute;
        //private readonly Func<object, bool> _canExecute;

        //public event EventHandler CanExecuteChanged;

        //public DelegateCommand(Action<object> execute, Func<object, bool> canExecute = null)
        //{
        //    _execute = execute ?? throw new ArgumentNullException(nameof(execute));
        //    _canExecute = canExecute;
        //}

        //public bool CanExecute(object parameter)
        //{
        //    return _canExecute == null || _canExecute(parameter);
        //}

        //public void Execute(object parameter)
        //{
        //    _execute(parameter);
        //}

        ///// <summary>
        ///// 手动触发 CanExecuteChanged，通知界面刷新按钮可用状态
        ///// </summary>
        //public void RaiseCanExecuteChanged()
        //{
        //    CanExecuteChanged?.Invoke(this, EventArgs.Empty);
        //}

        public bool CanExecute(object parameter)
        {
            if (this.CanExecuteFunc == null)
                return true;
            return this.CanExecuteFunc(parameter);
        }

        public event EventHandler CanExecuteChanged;

        public void Execute(object parameter)
        {
            if (this.ExecuteAction == null)
                return;
            this.ExecuteAction(parameter);
        }

        public DelegateCommand(Action<object> action)
        {
            this.ExecuteAction = action;
        }
        public Action<object> ExecuteAction { get; set; }
        public Func<object, bool> CanExecuteFunc { get; set; }
    }
}
